package com.meng.mmlive.ui.activity;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.RadioButton;

import com.meng.baselibrary.activity.BaseActivity;
import com.meng.mmlive.App;
import com.meng.mmlive.R;
import com.meng.mmlive.ui.fragment.RecentConversationListFragment;
import com.meng.mmlive.ui.fragment.TabFriendFragment;
import com.meng.mmlive.ui.fragment.TabHomeFragment;
import com.meng.mmlive.ui.fragment.TabMineFragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity implements View.OnClickListener {
    List<Class> fragmentClassList;
    RadioButton[] tabs;
    int[] tabIds = {R.id.tab_home,R.id.tab_calllog,R.id.tab_friend,R.id.tab_mine};
    FragmentManager fragmentManager;
    private int curIndex = -1;
    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_main);
    }
    @Override
    public void initView() {
        tabs = new RadioButton[tabIds.length];
        for(int i = 0;i < tabIds.length;i++){
            tabs[i] = (RadioButton) findViewById(tabIds[i]);
            tabs[i].setOnClickListener(this);
        }
    }

    @Override
    protected void initData() {
        initScreenWH();
        initFragments();
        fragmentManager = getSupportFragmentManager();
        switchTab(0);
    }

    /**
     * 切换fragment
     * @param index
     */
    private void switchTab(int index) {
        if(curIndex == index){
            return;
        }
        final String fragmentTag = fragmentClassList.get(index).getName();
        Fragment fragment = fragmentManager.findFragmentByTag(fragmentTag);
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        if(fragment == null){
            try {
                fragment = (Fragment) fragmentClassList.get(index).newInstance();
                transaction.add(R.id.fragment_container,fragment,fragmentTag);
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }else{
            transaction.show(fragment);
        }
        if(curIndex != -1){
            Fragment lastFragment = fragmentManager.findFragmentByTag(fragmentClassList.get(curIndex).getName());
            transaction.hide(lastFragment);
        }
        curIndex = index;
        transaction.commit();
    }

    /**初始化Fragment集合**/
    private void initFragments(){
        fragmentClassList = new ArrayList<>();
        fragmentClassList.add(TabHomeFragment.class);
//        fragmentClassList.add(TabCalllogFragment.class);
        fragmentClassList.add(RecentConversationListFragment.class);
        fragmentClassList.add(TabFriendFragment.class);
        fragmentClassList.add(TabMineFragment.class);
    }

    @Override
    public void onClick(View v) {
        for(int i = 0;i < tabs.length;i++){
            if(v == tabs[i]){
                switchTab(i);
                break;
            }
        }
    }

    /**
     * 初始化App中的屏幕宽高
     */
    private void initScreenWH(){
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        App.instance.screenW = dm.widthPixels;
        App.instance.screenH = dm.heightPixels;
    }
}
